package com.bjpowernode.front.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bjpowernode.api.service.UserService;
import com.bjpowernode.common.util.HttpUtils;
import com.bjpowernode.front.config.RealNameConfig;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class RealnameServiceImpl {

    @Resource
    private RealNameConfig realNameConfig;
    @DubboReference(interfaceClass = UserService.class,version = "1.0")
    private UserService userService;

    /*public boolean handleRealname(String phone,String name,String idCard){
        boolean realname = false;
        HashMap<String, String> params = new HashMap<>();
        params.put("cardNo",idCard);
        params.put("realName",name);
        try {
            String resp = HttpClientUtils.doGet(realNameConfig.getUrl(), params);
            if (StringUtils.isNotBlank(resp)){
                JSONObject respObject = JSONObject.parseObject(resp);
                if ("10000".equalsIgnoreCase(respObject.getString("code"))){
                    //解析result
                    realname = respObject.getJSONObject("result")
                            .getJSONObject("result")
                            .getBooleanValue("isok");
                    //处理更新数据库
                    boolean modifyResult = userService.modifyRealName(phone,name,idCard);
                    realname = modifyResult;
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return realname;
    };*/
    public boolean handleRealname(String phone,String name,String idCard) {
        boolean realname = false;
        String host = realNameConfig.getUrl();
        String path = "/verify_id_name";
        String method = "POST";
        String appcode = realNameConfig.getAppkey();
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("id_number", idCard);
        bodys.put("name", name);
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            String string = EntityUtils.toString(response.getEntity());
            JSONObject resultObject = JSONObject.parseObject(string);
            if (Objects.equals(resultObject.getString("state"), "1")){
                boolean modifyResult = userService.modifyRealName(phone, name, idCard);
                realname = modifyResult;
            }else {
                realname = false;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return realname;
    }

}
